package com.mycity.services;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.mycity.bean.Post;
import com.mycity.bean.User;
import com.mycity.dao.PostDao;
import com.mycity.dao.UserDao;

public class ProfileService {
	private UserDao userDao=new UserDao();
	private PostDao postDao=new PostDao();
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void setPostDao(PostDao postDao) {
		this.postDao = postDao;
	}
	
	public List<Post> getAllPostsByUserId(int userId) throws Exception{
		List<Post> postList;
		try {
			postList = postDao.retrievePostsByUserId(userId);


		} catch (Exception e) {
			throw new Exception(e);
		}
		return postList;
	}
	
	public int post(int userId, int postType, String postContent) throws Exception{
		String today = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date());
		int postId;
		try {
			postId=postDao.writePost(userId, postType, postContent, today);


		} catch (Exception e) {
			postId = -1;
		}
		return postId;
	}
	
	public List<Post> showRecentPosts() throws Exception{
		List<Post> postList;
		try {
			postList = postDao.recentPosts();
		} catch (Exception e) {
			throw new Exception(e);
		}
		return postList;
	}
	
	public Post getPostById(int id) throws Exception{
		Post post;
		try {
			post = postDao.getPost(id);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return post;
	}

}
